namespace AgentJohnson
{
  using System;
  using JetBrains.Annotations;
  using JetBrains.ReSharper.Feature.Services.CSharp.Bulbs;
  using Resharper.Abstractions.Actions;
  using Resharper.Abstractions.CodeDom.TypeMembers;
  using Resharper.Abstractions.CodeDom.TypeMembers.Methods;
  using Resharper.Abstractions.CodeDom.Types;

  /// <summary>Defines the <see cref="DuplicationMethod"/> class.</summary>
  [ContextAction("Duplicate method [Agent Johnson]", "Makes a duplicate of a method.")]
  public class DuplicationMethod : ContextActionBase
  {
    #region Constructors and Destructors

    /// <summary>Initializes a new instance of the <see cref="DuplicationMethod"/> class.</summary>
    /// <param name="provider">The provider.</param>
    public DuplicationMethod([NotNull] ICSharpContextActionDataProvider provider) : base(provider)
    {
    }

    #endregion

    #region Methods

    /// <summary>Determines whether this instance can execute the specified action context.</summary>
    /// <param name="context">The action context.</param>
    /// <returns><c>true</c> if this instance can execute the specified action context; otherwise, <c>false</c>.</returns>
    protected override bool CanExecute(CanExecuteContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      return this.GetModel(context) != null;
    }

    /// <summary>Executes the action.</summary>
    /// <param name="context">The execute context.</param>
    protected override void Execute(ExecuteContext context)
    {
      if (context == null)
      {
        throw new ArgumentNullException("context");
      }

      var model = this.GetModel(context);
      if (model == null)
      {
        return;
      }

      var code = model.Method.ToString();
      if (string.IsNullOrEmpty(code))
      {
        return;
      }

      var typeMember = context.Factory.CreateTypeMember(code);
      if (typeMember == null)
      {
        return;
      }

      var member = model.ContainingType.AddMember(typeMember, model.Method);
      if (member != null)
      {
        context.Formatter.Format(member);
      }
    }

    /// <summary>Gets the text.</summary>
    /// <returns>Returns the text.</returns>
    protected override string GetText()
    {
      return "Duplicate method [Agent Johnson]";
    }

    /// <summary>Gets the model.</summary>
    /// <param name="context">The context.</param>
    /// <returns>Returns the model.</returns>
    private Model GetModel(ProviderContext context)
    {
      var function = context.GetSelectedMethod();
      if (function == null)
      {
        return null;
      }

      var containingType = function.GetContainingType();
      if (containingType == null)
      {
        return null;
      }

      return new Model
      {
        Method = function,
        ContainingType = containingType
      };
    }

    #endregion

    /// <summary>Defines the <see cref="Model"/> class.</summary>
    private class Model
    {
      #region Properties

      /// <summary>
      /// Gets or sets the type of the containing.
      /// </summary>
      /// <value>The type of the containing.</value>
      [NotNull]
      public TypeElement ContainingType { get; set; }

      /// <summary>
      /// Gets or sets the function.
      /// </summary>
      /// <value>The function.</value>
      [NotNull]
      public Method Method { get; set; }

      #endregion
    }
  }
}